package com.example.aicooking.service.imp;

import com.example.aicooking.entity.UserDetail;
import com.example.aicooking.mapper.UserDetailMapper;
import com.example.aicooking.service.UserDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class UserDetailServiceImpl implements UserDetailService {

    @Autowired
    private UserDetailMapper userDetailMapper;

    // 查询健康信息
    @Override
    public UserDetail getUserDetail(Long userId) {
        return userDetailMapper.getUserDetailById(userId);
    }

    // 添加详细信息
    @Override
    public void addUserDetail(UserDetail userDetail) {
        // 计算BMI
        if (userDetail.getHeight() != null && userDetail.getWeight() != null) {
            float heightInMeter = userDetail.getHeight() / 100;
            float bmi = userDetail.getWeight() / (heightInMeter * heightInMeter);
            userDetail.setBmi(Math.round(bmi * 10) / 10f);
        }

        userDetailMapper.updateUserDetail(userDetail);
    }

    // 更新健康信息
    @Override
    public void updateUserDetail(UserDetail userDetail) {
        // 计算BMI
        if (userDetail.getHeight() != null && userDetail.getWeight() != null) {
            float heightInMeter = userDetail.getHeight() / 100;
            float bmi = userDetail.getWeight() / (heightInMeter * heightInMeter);
            userDetail.setBmi(Math.round(bmi * 10) / 10f);
        }

        userDetailMapper.updateUserDetail(userDetail);
    }
}